<template>
  <j-modal
    :title="$t('身份确认')"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okText="$t('确认无误')"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    :cancelText="$t('关闭')">
    <a-spin :spinning="confirmLoading">
    <div>
      <a-form-model ref="form" :model="model" :labelCol="labelCol" :wrapperCol="wrapperCol">
        <div style="margin-bottom: 14px; color: orange">
          {{ $t('说明：运营参数直接关系到场地的运营，若修改需要场地负责人验证。') }}
        </div>
        <a-form-model-item :label="$t('负责人')" style="margin-bottom: 0">
          <span style="font-size: 16px; color: #000">{{ model.name }}</span>
        </a-form-model-item>
        <a-form-model-item v-if="model.typeCode_dictText !== '6'" :label="$t('联系电话')" style="margin-bottom: 0">
          <span style="font-size: 16px; color: #000">{{ model.phone | hidephone }}</span>
        </a-form-model-item>
        <a-form-model-item v-if="model.typeCode_dictText === '6'" :label="$t('联系邮箱')" style="margin-bottom: 0">
          <span style="font-size: 16px; color: #000">{{ model.email | hideEmail }}</span>
        </a-form-model-item>
        <a-form-model-item :label="$t('验证码')" prop="captcha" :rules="[{ required: true, message: $t('请输入验证码') }]">
          <div>
            <a-input
              style="width: 200px"
              v-model="model.captcha"
              ref="captcha"
              :maxLength="6"
              oninput="value=value.replace(/\D/g,'');"
            />
            <a-button
              type="primary"
              style="color: #fff; background-color: #ff6550; border-color: #ff6550"
              @click="getCaptcha"
              :disabled="isTiming"
              >{{ btnfont }}</a-button
            >
          </div>
        </a-form-model-item></a-form-model
      >
    </div>
  </a-spin>
  </j-modal>
</template>

<script>
import Vue from 'vue'
import { TENANT_ID } from '@/store/mutation-types'
import { getAction, postAction } from '@/api/manage'
let timer, i

  export default {
    name: 'YarnCertOutCaptchaModal',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false,
      },
    },
    data () {
      return {
        title:'',
        width:600,
        visible: false,
        disableSubmit: false,
        model: {
          name: '',
          phone: '',
          email:'',
          typeCode_dictText:"1"
        },
        confirmLoading: false,
        btnfont: this.$t('获取'),
        isTiming: false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        param:null,
        callBack:null,
        tenantId: Vue.ls.get(TENANT_ID),
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))
    },
    methods: {
      show(_param) {
        let that = this
        that.param=_param;
        this.visible=true
        // 添加code查询，兼容总部授权
        if(!this.tenantId){
          this.tenantId = 0
        }
        console.log("----------"+this.tenantId)
        getAction('/shop/bisShop/list', {code : this.tenantId}).then((res) => {
          if (res.success) {
            if (res.result.total > 0) {
              let dtl = res.result.records[0]
              let name = dtl.contact || ''
              let phone = dtl.phone || ''
              let email = dtl.email || ''
              let typeCode_dictText = dtl.typeCode_dictText
              if (typeCode_dictText === '1'){
                if (!phone || !name) {
                  that.$message.warning(that.$t('请联系新盈加客服填写场地负责人信息'))
                  that.isTiming = true
                  return
                }
              }
              else{
                if (!email || !name) {
                  that.$message.warning(that.$t('请联系新盈加客服填写场地负责人信息'))
                  that.isTiming = true
                  return
                }
              }
              if(i === undefined || i === 0){
                that.isTiming = false
              }else{
                that.isTiming = true
              }
              that.$nextTick(() => {
                that.model = Object.assign({}, { name: name, phone: phone ,email:email,typeCode_dictText:typeCode_dictText})
              })
            }
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      showAndCall(syncParam, callBack) {
        let that = this
        that.callBack=callBack;
        that.param=syncParam;
        this.visible=true
        // 添加code查询，兼容总部授权
        if(!this.tenantId){
          this.tenantId = 0
        }
        getAction('/shop/bisShop/list', {code : this.tenantId}).then((res) => {
          if (res.success) {
            if (res.result.total > 0) {
              let dtl = res.result.records[0]
              let name = dtl.contact || ''
              let phone = dtl.phone || ''
              let email = dtl.email || ''
              let typeCode_dictText = dtl.typeCode_dictText
              if (typeCode_dictText === '1'){
                if (!phone || !name) {
                  that.$message.warning(that.$t('请联系新盈加客服填写场地负责人信息'))
                  that.isTiming = true
                  return
                }
              }
              else{
                if (!email || !name) {
                  that.$message.warning(that.$t('请联系新盈加客服填写场地负责人信息'))
                  that.isTiming = true
                  return
                }
              }
              if(i === undefined || i === 0){
                that.isTiming = false
              }else{
                that.isTiming = true
              }
              that.$nextTick(() => {
                that.model = Object.assign({}, { name: name, phone: phone ,email:email,typeCode_dictText:typeCode_dictText})
              })
            }
          } else {
            that.$message.warning(res.message)
          }
        })
      },
      getCaptcha() {
        let that = this;
        i = 60
        that.isTiming = true
        // that.$message.warning(that.$t('send-sms',{phone:that.model.phone.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2")}))
        // let request = {}
        // request['phone'] = that.model.phone
        getAction('/guest/user/sendAuthCode', {}).then((res) => {
          if (res.success) {
            clearInterval(timer)
            timer = setInterval(function () {
              if (i == 0) {
                clearInterval(timer)
                that.btnfont = that.$t('获取')
                that.isTiming = false
              } else {
                i--
                that.btnfont = i + 's'
              }
            }, 1000)
          } else {
            that.$message.warning(res.message)
            that.isTiming = false
          }
        })
      },
      close () {
        this.visible = false;
      },
      handleOk () {
        let that = this
        that.$refs.form.validate((valid) => {
          if (valid) {
            if (that.callBack!=null) {
              //that.close();
              that.callBack(that,that.param, that.model.captcha);
              return;
            }
            let result = that.param;
            result.captcha = that.model.captcha;

            //that.$emit("callBack", result);
            this.$emit('ok', result);

            //this.visible = false;
          }
        })
      },
      handleCancel () {
        this.$emit('cancel');
        this.close()
      }
    }
  }
</script>